package org.stvd.controller.workflow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.Consts;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Department;
import org.stvd.entities.admin.Users;
import org.stvd.entities.workflow.WfUser;
import org.stvd.service.admin.DepartmentService;
import org.stvd.service.admin.UsersService;
import org.stvd.service.workflow.WfUserService;

/**
 * @Destribution 流程审批用户管理类
 * @author wwh
 * @date 2020年11月10日
 */
@Controller
@RequestMapping("/workflow/user")
public class UserController extends BaseController {

	@Autowired
	private WfUserService wfUserService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private UsersService usersService;

	private String tmURL = "/workflow/user/";

	private Integer localPageSize = 10;

	/**
	 * 流程审批用户管理页面
	 * 
	 * @param gcode  分组编号
	 * @param wfUser 流程审批用户实体类
	 * @return
	 */
	@RequestMapping("manage")
	public String usermanage(@ModelAttribute("wfUser") WfUser wfUser) {
		List<String> list = new ArrayList<>();
		// 机构信息
		List<Department> depList = departmentService.findAll(Department.class);
		map.put("depList", depList);
		// 用户所在机构信息
		List<Map<String, Object>> userDepList = departmentService.listUserDepByGuid(Consts.GUID);
		map.put("userDepList", userDepList);
		for (Department department : depList) {
			list.add(department.getDepId());
		}
		QueryResult<Users> userList = usersService.getUsersQueryResult(0, 0, "", "", list, "", Consts.GUID);
		map.put("userList", userList.getRows());
		QueryResult<Map<String, Object>> queryResult = wfUserService.wfUserQueryResultByName((pageNo - 1) * pageSize,
				pageSize, wfUser.getUserName(), "");
		map.put("queryResult", queryResult);
		return tmURL + "UserManage.html";
	}

	/**
	 * @Destribution 新增流程审批用户
	 * @param gcode
	 * @param userId
	 * @return
	 */
	@PostMapping("add")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> addUserSubmit(
			@RequestParam(value = "ids", required = false) String ids) {
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		result.transferResult(wfUserService.insertWfUser(ids));
		return result;
	}

	/**
	 * @Destribution 删除分组用户
	 * @param id
	 * @return
	 */
	@Log(title = "删除审批用户信息", businessType = BusinessType.DELETE)
	@PostMapping("del")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> deleteUser() {
		String id = request.getParameter("id");
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		try {
			wfUserService.deleteWfUser(Long.valueOf(id));
			result.setMsg("删除成功！");
		} catch (Exception e) {
			result.setMsg("删除失败！<br/>失败原因：" + e.getMessage());
		}
		return result;
	}

	@RequestMapping(value = "select/{multiple}/{ids}")
	public String userchoosemanage(@PathVariable String multiple, @PathVariable String ids) {
		List<String> list = new ArrayList<>();
		List<Department> depList = departmentService.findAllDepByGuid(Consts.GUID);
		map.put("depList", depList);
		String dep = request.getParameter("dep");
		if (StringUtil.isEmpty(dep)) {
			for (Department department : depList) {
				list.add(department.getDepId());
			}
		} else {
			list.add(dep);
		}
		QueryResult<Map<String, Object>> usersQueryResult = wfUserService
				.wfUserQueryResultByName((pageNo - 1) * localPageSize, localPageSize, "", "");
		map.put("queryResult", usersQueryResult);
		map.put("multiple", multiple);
		map.put("ids", ids);
		map.put("userList", usersQueryResult.getRows());
		map.put("totalPageNum", usersQueryResult.getPageCount());
		map.put("currentPage", usersQueryResult.getPageIndex());
		map.put("pageSize", localPageSize);
		return tmURL + "UserList.html";
	}

	@RequestMapping(value = "names")
	@ResponseBody
	public Map<String, Object> getUnames() {
		Map<String, Object> data = new HashMap<String, Object>();
		String ids = request.getParameter("ids");
		String unames = "";
		if (ids != null && ids.length() > 0) {
			String[] uidList = ids.split(",");
			for (int i = 0; i < uidList.length; i++) {
				Users user = usersService.findById(uidList[i]);
				unames = unames.concat(user.getUname() + ",");
			}
			unames.endsWith(",");
			unames = unames.substring(0, unames.length() - 1);
		}
		data.put("name", unames);
		return data;
	}

	@RequestMapping(value = "loadData")
	@ResponseBody
	public Map<String, Object> loadData() {
		String pageNo = request.getParameter("pageNo");
		String name = request.getParameter("name");
		if (name == null) {
			name = "";
		}
		Integer pageNum = 1;
		if (!StringUtil.isEmpty(pageNo)) {
			pageNum = Integer.parseInt(pageNo);
		}
		Map<String, Object> data = new HashMap<String, Object>();
		QueryResult<Map<String, Object>> usersQueryResult = wfUserService
				.wfUserQueryResultByName((pageNum - 1) * localPageSize, localPageSize, "", "");
		data.put("queryResult", usersQueryResult);
		data.put("userList", usersQueryResult.getRows());
		data.put("totalPageNum", usersQueryResult.getPageCount());
		data.put("currentPage", usersQueryResult.getPageIndex());
		data.put("pageSize", localPageSize);
		return data;
	}
}
